package com.tj.test.sys.service.impl;

import com.tj.test.sys.dao.PowerMapper;
import com.tj.test.sys.po.Power;
import com.tj.test.sys.service.IPowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*实现业务及流程接口当中的内容*/
@Service
public class PowerService implements IPowerService {
    @Autowired
    private PowerMapper mapper;

    @Override
    //查询所有权限信息
    public List<Power> selectAllPower() {
        List<Power> list = new ArrayList<>();
        if (!mapper.selectAllPower().isEmpty()) {
            list = mapper.selectAllPower();
        }
        return list;
    }

    @Override
    //插入权限信息
    public int insertPower(Power power) {
        int temp = 0;
        /* 业务判断
         * （1）权限名称不能重复
         * （2）增加权限之前先查询*/
        temp = mapper.selectPowerByIdCount(power);
        if (temp < 1) {
            mapper.insertPower(power);
        }
        return temp;
    }

    @Override
    //删除权限信息
    public int deletePower(Power power) {
        int temp = 0;
        /*1.有关业务流程的判断*/
        //(1)删除有关业务
        try {
            temp = mapper.selectPowerByIdCount(power);
            if (temp > 0) {
                mapper.deletePower(power);
            }
        } catch (Exception e) {
            temp = -1;
            System.out.println(e.getStackTrace());
        }

        return temp;
    }

    /**
     * 修改权限信息
     *
     * @param power 权限信息实体
     * @return
     */
    @Override

    public int updatePower(Power power) {
        int temp = 0;
        try {
            temp = mapper.selectPowerByIdCount(power);
            if (temp > 0) {
                mapper.updatePower(power);
            }
        } catch (Exception e) {
            temp = -1;
            //打印错误信息
            System.out.println(e.getStackTrace());
        }

        return temp;
    }
}